#include "stdafx.h"
#include "ManagedDnsRecord.h"

using namespace System;
using namespace Adaptive::Network::Dns;

ManagedDnsRecord::ManagedDnsRecord(void)
{
	_name = nullptr;
	_recordType = DnsQueryType::Address;
	_dataLength = 0;
	_recordFlags = 0;
	_ttl = 0;
	_reservedValue = 0;

	_addressRecord = nullptr;
	_soaRecord = nullptr;
	_pointerRecord = nullptr;
	_mailInfoRecord = nullptr;
	_mailExchangeRecord = nullptr;
	_textRecord = nullptr;
	_nullRecord = nullptr;
	_wksRecord = nullptr;
	_ip6Record = nullptr;
	_keyRecord = nullptr;
	_signatureRecord = nullptr;
	_aTMAddressRecord = nullptr;
	_nextRecord = nullptr;
	_srvRecord = nullptr;
	_tKeyRecord = nullptr;
	_tSignatureRecord = nullptr;
	_winSRecord = nullptr;
	_winSRRecord = nullptr;
}
ManagedDnsRecord::ManagedDnsRecord(DNS_RECORD* originalRecord)
{
	if (originalRecord == NULL)
	{
		_name = nullptr;
		_recordType = DnsQueryType::Address;
		_dataLength = 0;
		_recordFlags = 0;
		_ttl = 0;
		_reservedValue = 0;
	}
	else
	{
		_name = gcnew String (originalRecord->pName);
		_recordType = (DnsQueryType)originalRecord->wType;
		_dataLength = originalRecord->wDataLength;
		_recordFlags = (int)originalRecord->Flags.DW;
		_ttl = originalRecord->dwTtl;
		_reservedValue = originalRecord->dwReserved;
	}

	_addressRecord = DnsRecordFactory::CreateDnsAddressRecord(originalRecord);
	_soaRecord = DnsRecordFactory::CreateDnsStartOfAuthorityRecord(originalRecord);
	_pointerRecord = DnsRecordFactory::CreateDnsPointerRecord(originalRecord);
	_mailInfoRecord = DnsRecordFactory::CreateDnsMailInformationRecord(originalRecord);
	_mailExchangeRecord = DnsRecordFactory::CreateDnsMailExchangeRecord(originalRecord);
	_textRecord = DnsRecordFactory::CreateDnsTextRecord(originalRecord);
	_nullRecord = DnsRecordFactory::CreateDnsNullRecord(originalRecord);
	_wksRecord = DnsRecordFactory::CreateDnsWellKnownServiceRecord(originalRecord);
	_ip6Record = DnsRecordFactory::CreateDnsIp6AddressRecord(originalRecord);
	_keyRecord = DnsRecordFactory::CreateDnsKeyRecord(originalRecord);
	_signatureRecord = DnsRecordFactory::CreateDnsSignatureRecord(originalRecord);
	_aTMAddressRecord = DnsRecordFactory::CreateDnsATMAddressRecord(originalRecord);
	_nextRecord = DnsRecordFactory::CreateDnsNextRecord(originalRecord);
	_srvRecord = DnsRecordFactory::CreateDnsServiceRecord(originalRecord);
	_tKeyRecord = DnsRecordFactory::CreateDnsTKeyRecord(originalRecord);
	_tSignatureRecord = DnsRecordFactory::CreateDnsTSignatureRecord(originalRecord);
	_winSRecord = DnsRecordFactory::CreateDnsWINSRecord(originalRecord);
	_winSRRecord = DnsRecordFactory::CreateDnsWINSReverseLookupRecord(originalRecord);
}
ManagedDnsRecord::~ManagedDnsRecord(void)
{
	delete(_addressRecord);
	delete(_soaRecord);
	delete(_pointerRecord);
	delete(_mailInfoRecord);
	delete(_mailExchangeRecord);
	delete(_textRecord);
	delete(_nullRecord);
	delete(_wksRecord);
	delete(_ip6Record);
	delete(_keyRecord);
	delete(_signatureRecord);
	delete(_aTMAddressRecord);
	delete(_nextRecord);
	delete(_srvRecord);
	delete(_tKeyRecord);
	delete(_tSignatureRecord);
	delete(_winSRecord);
	delete(_winSRRecord);

	_addressRecord = nullptr;
	_soaRecord = nullptr;
	_pointerRecord = nullptr;
	_mailInfoRecord = nullptr;
	_mailExchangeRecord = nullptr;
	_textRecord = nullptr;
	_nullRecord = nullptr;
	_wksRecord = nullptr;
	_ip6Record = nullptr;
	_keyRecord = nullptr;
	_signatureRecord = nullptr;
	_aTMAddressRecord = nullptr;
	_nextRecord = nullptr;
	_srvRecord = nullptr;
	_tKeyRecord = nullptr;
	_tSignatureRecord = nullptr;
	_winSRecord = nullptr;
	_winSRRecord = nullptr;
}

String^ ManagedDnsRecord::Name::get()
{
	return _name;
}
DnsQueryType ManagedDnsRecord::RecordType::get()
{
	return _recordType;
}
int ManagedDnsRecord::RecordFlags::get()
{
	return _recordFlags;
}
short ManagedDnsRecord::DataLength::get()
{
	return _dataLength;
}
int ManagedDnsRecord::ReservedValue::get()
{
	return _reservedValue;
}
int ManagedDnsRecord::Ttl::get()
{
	return _ttl;
}
DnsAddressRecord ManagedDnsRecord::AddressRecord::get()
{
	return *(_addressRecord);
}

DnsIp6AddressRecord ManagedDnsRecord::Ip6Record::get()
{
	return *(_ip6Record);
}
DnsATMAddressRecord ManagedDnsRecord::ATMAddressRecord::get()
{
	return *(_aTMAddressRecord);
}
DnsKeyRecord ManagedDnsRecord::KeyRecord::get()
{
	return *(_keyRecord);
}
DnsMailInformationRecord ManagedDnsRecord::MailInfoRecord::get()
{
	return *(_mailInfoRecord);
}
DnsMailExchangeRecord ManagedDnsRecord::MailExchangeRecord::get()
{
	return *(_mailExchangeRecord);
}
DnsNullRecord ManagedDnsRecord::NullRecord::get()
{
	return *(_nullRecord);
}
DnsPointerRecord ManagedDnsRecord::PointerRecord::get()
{
	return *(_pointerRecord);
}
DnsSignatureRecord ManagedDnsRecord::SignatureRecord::get()
{
	return *(_signatureRecord);
}
DnsStartOfAuthorityRecord ManagedDnsRecord::SoaRecord::get()
{
	return *(_soaRecord);
}
DnsTextRecord ManagedDnsRecord::TextRecord::get()
{
	return *(_textRecord);
}
DnsWellKnownServiceRecord ManagedDnsRecord::WksRecord::get()
{
	return *(_wksRecord);
}
DnsNextRecord ManagedDnsRecord::NextRecord::get()
{
	return *(_nextRecord);
}
DnsServiceRecord ManagedDnsRecord::SrvRecord::get()
{
	return *(_srvRecord);
}
DnsTSignatureRecord ManagedDnsRecord::TSignatureRecord::get()
{
	return *(_tSignatureRecord);
}
DnsWINSRecord ManagedDnsRecord::WinSRecord::get()
{
	return *(_winSRecord);
}
DnsWINSReverseLookupRecord ManagedDnsRecord::WinSRRecord::get()
{
	return *(_winSRRecord);
}
DnsTKeyRecord ManagedDnsRecord::TKeyRecord::get()
{
	return *(_tKeyRecord);
}
System::Object^	ManagedDnsRecord::DataRecord::get()
{
	System::Object^ returnValue = nullptr;

	switch(_recordType)
	{
	case DnsQueryType::Address:
		returnValue = _addressRecord;
		break;

	case DnsQueryType::StartOfAuthority:
		returnValue = _soaRecord;
		break;

	case DnsQueryType::DomainPointer:
	case DnsQueryType::NameServer:
	case DnsQueryType::CanonicalName:
	case DnsQueryType::MailBoxCode:
	case DnsQueryType::MailGroupMember:
	case DnsQueryType::MailDestination:
	case DnsQueryType::MailRename:
		returnValue = _pointerRecord;
		break;

	case DnsQueryType::MailboxInfo:
		returnValue = _mailInfoRecord;
		break;

	default:
		returnValue = nullptr;
		break;
	}

	return returnValue;
}
